home *** CD-ROM | disk | FTP | other *** search
/ PC Answers 2002 January / PC Answers January 2002.7z / PC Answers January 2002.bin / graphics / freepixl / _SETUP.1 / Com1com2.pxl < prev    next >
Text File  |  2001-10-11  |  10KB  |  318 lines

  1. Initialize:
  2.     Title$ = "Serial Communications Control"
  3.     UseCaption(Title$)
  4.     UseCoordinates(PIXEL)
  5.     SetColorPalette(BITMAP)
  6.     UseBackground(TRANSPARENT,166,166,166)
  7.     DrawBackground
  8.     WinLocate(Title$,300,200,790,500,Res)
  9.     WinGetClientRect("",cx1,cy1,cx2,cy2)
  10.     DirGet(SourceDir$)
  11.     ImageDir$ = SourceDir$
  12.  
  13.     WinShow(Title$,TOPMOST,Res)
  14.     InfoMenu(REMOVE)
  15.     WaitInput(100)
  16.     SetMenu("E&xit!",Leave,
  17.         ENDPOPUP,
  18.         "&Devices",IGNORE,
  19.         "Set COM port", SettingComms,
  20.         "Digitize", Digitize,
  21.         SEPARATOR,
  22.         "Interogate",Interogate,
  23.         ENDPOPUP)
  24.     ChangeMenuItem("Digitize",GRAY,Res)
  25.  
  26. Wait_for_Input: 
  27.     WaitInput()
  28.  
  29.  
  30. Leave:
  31.     End
  32.  
  33. Interogate:
  34.     DrawBackground
  35.     MessageBox(OKCANCEL,1,INFORMATION, "If no device is attached, PiXCL will wait with the hourglass cursor. Menus are still active. Do you want to continue?",
  36.     "PiXCL expects a device to be attached to the COM1 port",Res)
  37.     If Res = 1
  38.         SetCommPort(COM1,"baud=9600 parity=N data=8 stop=1",XON,Res)
  39.         WriteCommPort(COM1,"s")
  40.         UseCursor(WAIT)
  41.         WaitCommEvent(R,COM1,Bitpad3,0)
  42.     Endif
  43.     Goto Wait_for_Input
  44.  
  45. SettingComms:
  46.     DrawBackground
  47.     ModeParms$ = "baud=9600 parity=E data=7 stop=2"
  48.     SetCommPort(COM1,ModeParms$,XON,Res)
  49. DebugMsgBox(Res)
  50.     If Res = 0 Then DrawText(10,10,"Set COM1 state Failed")
  51.     GetCommPort(COM1,Baud,DataBits,Parity,StopBits,XonXoff)
  52.  
  53.     DrawNumber(10,30,Baud)  DrawText(60,30,"baud rate")
  54.     DrawNumber(10,50,DataBits)  DrawText(40,50,"data bits 4-8")
  55.     DrawNumber(10,70,Parity)  DrawText(40,70,"parity 0-4=no,odd,even,mark,space")
  56.     DrawNumber(10,90,StopBits)  DrawText(40,90,"stop bits 0,1,2 = 1, 1.5, 2")
  57.     DrawNumber(10,110,XonXoff)  DrawText(40,110,"Xon/Xoff  1 = Xon")
  58.     ChangeMenuItem("Digitize",ENABLE,Res)
  59.     Goto Wait_for_Input
  60.  
  61. Digitize:
  62.     ClearCommPort(COM1)
  63.     Count = 0
  64.     Y1 = 10
  65.     {Next is a command for some digitizer pads. Consult your documentation.}
  66.     WriteCommPort(COM1,"Po") 
  67.     UseCursor(WAIT)
  68.     WaitCommEvent(R,COM1,Bitpad2,0)  {starts a com port watch thread}
  69.     Goto Wait_for_Input
  70.  
  71. Bitpad2:
  72.     UseCursor(ARROW)
  73.     If Count = 0 Then DrawBackground
  74.     ReadCommPort(COM1,BitpadData$)
  75.     Beep
  76.     Len(BitpadData$,Long)
  77.     If Long > 0
  78.         Long -= 2
  79.         Left(BitpadData$,Long,BitpadData$)
  80.         DrawText(10,Y1,BitpadData$)
  81.         Y1 += 20
  82.         Count++
  83.     Endif 
  84.     If Count < 10
  85.         UseCursor(WAIT)
  86.         WaitCommEvent(R,COM1,Bitpad2,0)
  87.     Else
  88.         ClearCommPort(COM1)
  89.     Endif
  90.     Goto Wait_for_Input
  91.  
  92. Bitpad3:
  93.     ReadCommPort(COM1,BitpadData$)
  94.     Beep
  95.     UseCursor(ARROW)
  96.     Len(BitpadData$,Long)
  97.     If Long > 0
  98.         Long -= 2
  99.         Left(BitpadData$,Long,BitpadData$)
  100.         DrawText(10,10,BitpadData$)
  101.     Endif
  102.     ClearCommPort(COM1)
  103.     Goto Wait_for_Input
  104.  
  105.  
  106. {
  107. Serial Communications with PiXCL
  108.  
  109. PiXCL provides programmed read/write access to your PC serial ports, COM1, COM2, COM3 
  110. and COM4, for purposes such as reading data streams from input devices like digitising tables, 
  111. and commanding or reading devices that are controlled from an RS-232 port. These can be as 
  112. diverse as smart home appliances to model trains. 
  113.  
  114. Most PCs have two serial ports only, COM1 and COM2, with COM2 often assigned to a modem 
  115. for Internet access. Some PCs use COM1 for the mouse input, while most newer PCs have a 
  116. PS2 style mouse port, which leaves COM1 available for other devices.
  117.  
  118. The PC design supports four standard serial ports which share only two interrrupt lines. Hence, 
  119. COM1 and COM3 share one, and COM2 and COM4 share the other. This means that if you want 
  120. install two serial cards and use two serial  devices on COM1 and COM3, your software will have 
  121. to be able to support shared interrupts.
  122.  
  123. COM port support in PiXCL is not a device driver, and does not support shared interrupts.
  124.  
  125.  
  126.  
  127. Commands:
  128.  
  129. GetCommPort
  130. Gets the current baudrate, parity etc
  131.  
  132. Syntax: GetCommPort(COMx,Baud,DataBits,Parity,StopBits,XonXoff)
  133.  
  134. Parameters:
  135. COMx    Port,where x = 1 - 4
  136. Baud    
  137. DataBits    
  138. Parity    
  139. StopBits    
  140. XonXoff    Data stream flow control. XOFF =0, XON = 1
  141.  
  142.  
  143.  
  144. ======================================
  145. SetCommPort    
  146.  
  147. This command requires a string that specifies device-control information. The string must have 
  148. the same form as the mode commandÆs command-line arguments. For example, the following 
  149. string specifies a baud rate of 1200, no parity, 8 data bits, and 1 stop bit: 
  150.  
  151. baud=1200 parity=N data=8 stop=1 
  152.  
  153. The device name is ignored if it is included in the string, but it must specify a valid device, as follows: 
  154.  
  155. COM1: baud=1200 parity=N data=8 stop=1 
  156.  
  157. This command string can also be written with comma delimiters as
  158. 1200,N,8,1
  159.  
  160. For further information on mode command syntax, refer to the end-user documentation for your 
  161. operating system. 
  162.  
  163. Syntax: SetCommPort(COMx,Mode$,XON|XOFF,Result)
  164.  
  165. Parameters:
  166. COMx    Port,where x = 1 - 4
  167.  
  168. Mode$    Port state parameters, according to the MS-DOS mode command, e.g.
  169.     "baud=9600 parity=N data=8 stop=2"
  170.  
  171. XON|XOFF     Enables or disables the data stream control mode for both input and output.
  172.  
  173. Result    1 if the command was successful, otherwise 0.
  174.  
  175. Remarks:
  176. You can set the COMx port parameters from the control panel for system wide operation. The settings 
  177. made within PiXCL only apply while PiXCL is running. 
  178.  
  179. ======================================
  180. ClearCommPort
  181. This command clears the selected comm port.
  182.  
  183. Syntax: ClearCommPort(COMx)
  184.  
  185. Parameters:
  186. COMx    Port,where x = 1 - 4
  187.  
  188. Remarks:
  189. The same effect can be achieved by sending SetCommPort.
  190.  
  191. ======================================
  192.  
  193. ReadCommPort - reads stream into string variable
  194.  
  195. Asynchronous data streams typically include variable length data, followed by a terminating 
  196. character or string.  Most useful serial devices like digitizing tablets let you configure the 
  197. terminating string. In the current version of PiXCL, this string MUST be set to a carriage 
  198. return and linefeed pair (CR-LF). 
  199.  
  200. PiXCL maintains a read buffer of about 4 KB. Most inout will be far less than this size. When the CR-LF 
  201. is received, a comms event is generated to which your PiXCL program can respond, in much the same 
  202. way that your program responds to mouse or keyboard events in the WaitInput() idle loop.
  203.  
  204. Hence, to receive a read or write comms event, you would use a command
  205. WaitCommsEvent(R,<label>,Timeout)
  206.  
  207. at <label>, you would use a ReadCommPort command to get the current buffer.
  208.  
  209. Syntax: ReadCommPort(COMx,Data$)
  210.  
  211. Parameters:
  212. COMx    Port,where x = 1 - 4
  213.  
  214. Data$    The data read in from the port buffer
  215.  
  216. ======================================
  217.  
  218. WriteCommPort
  219.  
  220. Asynchronous data streams typically include variable length data, followed by a terminating 
  221. character or string.  Most useful serial devices like digitizing tablets let you configure the 
  222. terminating string. In the current version of PiXCL, this string MUST be set to a carriage 
  223. return and linefeed pair (CR-LF). 
  224.  
  225. PiXCL maintains a write buffer of about 4 KB. When the last character in the buffer is written to the 
  226. port,  a comms event is generated to which your PiXCL program can respond, in much the same way 
  227. that your program responds to mouse or keyboard events in the WaitInput() idle loop.
  228.  
  229. Hence, to receive a write comms event, you would use a command
  230. WaitCommsEvent(W,<label>,Timeout) 
  231.  
  232. Syntax: ReadCommPort(COMx,Data$)
  233.  
  234. Parameters:
  235. COMx    Port,where x = 1 - 4
  236.  
  237. Data$    The data read in from the port buffer. This includes the CR-LF terminator string, or may be 
  238.     just a CR-LF. Once the buffer is read, it is cleared.
  239.  
  240. ======================================
  241.  
  242. WaitCommEvent
  243.  
  244.  
  245. Syntax: WaitCommEvent(R|W,Label,Timeout) 
  246.  
  247. Parameters:
  248. R | W    R: wait for a CR-LF terminator to be received.
  249.     W: wait till the last character in the buffer is written. If the serial device written has to do 
  250.           some processing, this may take a few seconds.
  251.  
  252. Label    The jump-to location in your script to process the event.
  253.  
  254. Timeout    The timelimit after which an event is issued anyway. This may happen if the attached 
  255.     device is not responding, so your program will have to be able to handle this situation. 
  256.     For example, if a Read timeout occurs, the buffer will be empty, and ReadCommPort will
  257.     return a NULL string. Timeout has a default value of 5000 mS  (i.e. 5 minutes). If you set Timeout 
  258.     to <= 0, it defaults to 5000.
  259.  
  260. Remarks:
  261. WaitCommsEvent will interact with the WaitInput() idle loop. If a WaitCommsEvent is active, and a mouse 
  262. or keyboard event occurs, this will be processed immediately.  You can set a flag variable to indictate that 
  263. a WaitCommsEvent is active, and put a WaitCommsEvent into a temporary idle loop.
  264.  
  265.  
  266.  
  267.  
  268.  
  269. Example using a digitizing tablet.
  270. Step #1: Set the tablet baud rate and character framing, for example "9600,N,8,1", with XON flow control.
  271.  
  272. Step#2: Connect the tablet to the COM1 port on your PC. Ensure that the receive and transmit lines 
  273. (pins 2 and 3) connect PC receive to tablet transmit and vice-versa. If necessary, refer to the PC and 
  274. tablet documentation for more information.
  275.  
  276. Step#3: Insert a command in yoru PiXCL program like
  277.     SetCommPort(COM1,"baud=9600 parity=N data=8 stop=1",XON,Res)  or
  278.     SetCommPort(COM1,"9600,N,8,1",XON,Res)
  279.  
  280.  
  281.  
  282. EscCommFunction
  283.  
  284. This commands sends escape functions to the selected COM port.
  285.  
  286. Syntax: EscCommFunction(COMx, ESC_token)
  287.  
  288. Parameters:
  289. COMx    Port,where x = 1 - 4
  290.  
  291. ESC_token
  292. Specifies the code of the extended function to perform. This parameter can be one 
  293. of the following values: 
  294.  
  295. Value     Meaning 
  296.  
  297. CLRDTR     Clears the DTR (data-terminal-ready) signal.  
  298.  
  299. CLRRTS     Clears the RTS (request-to-send) signal. 
  300.  
  301. SETDTR     Sends the DTR (data-terminal-ready) signal. 
  302.  
  303. SETRTS     Sends the RTS (request-to-send) signal. 
  304.  
  305. SETXOFF Causes transmission to act as if an XOFF character has been received. 
  306.  
  307. SETXON     Causes transmission to act as if an XON character has been received. 
  308.  
  309. SETBREAK     Suspends character transmission and places the transmission line 
  310. in a break state until the CLRREAK extended function code). Note that this extended 
  311. function does not flush data that has not been transmitted. 
  312.  
  313. CLRBREAK     Restores character transmission and places the transmission line in 
  314. a nonbreak state. The CLRBREAK extended function code is identical to the ClearCommPort 
  315. function. 
  316.  
  317.  
  318. }